package cn.oowoo.sentinelclusterclientserver.confroller;

import cn.oowoo.sentinelclusterclientserver.service.TestService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SentinelTest {
    @Autowired
    private TestService testService;

    @GetMapping("/hello/{name}")
    public String apiHello(@PathVariable String name) throws Exception {
        return testService.apiHello(name);
    }



    static class Test{
        // 注意这里的port为我们应用服务器的启动port，对应应用启动参server.port
        public static final String requestPath = "http://localhost:7000/hello/zhangsan";
        public static final String requestPath2 = "http://localhost:7001/hello/zhangsan";
        public static final String requestPath3 = "http://localhost:7002/hello/zhangsan";

        private static RestTemplate restTemplate = new RestTemplate();
        private static RestTemplate restTemplate2 = new RestTemplate();
        private static RestTemplate restTemplate3 = new RestTemplate();

        public static void main(String[] args) {

            int i = 0;
            while (true) {
                executeGet();
                i++;
                if (i % 10 == 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (i == 200000) {
                    break;
                }
            }
        }

        // 连续执行get请求
        private static void executeGet() {
            restTemplate.getForEntity(requestPath, String.class);
            restTemplate2.getForEntity(requestPath2, String.class);
            restTemplate3.getForEntity(requestPath3, String.class);
        }

    }



}
